package com.ww.springboot.boot.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.IntStream;

/**
 * 描述：
 *
 * @author wanwei
 * @since 2021-08-05 10:39
 */
@RestController
@RequestMapping("/jvm")
@Slf4j
public class JvmController {

    private static final int _1MB = 1024 * 1024;

    //private static final Map<Integer, JvmCreateClassDTO> map = new HashMap<>();

    @RequestMapping("/test")
    public String test(String param) {
        System.out.println("--------------");

        switch (param) {
            case "1":
                Long t1 = System.currentTimeMillis();
                method();
                System.out.println(System.currentTimeMillis() - t1);
                break;
            default:
        }
        return "success";
    }

    @RequestMapping("/test2")
    public String test2(String param) {
        System.out.println("--------------");

        switch (param) {
            case "1":
                //map.clear();
                break;
            default:
        }
        return "success";
    }

    /**
     * 创建大量对象 但是不用
     */
    private void method() {
        IntStream.range(0, 1000000).boxed().parallel().forEach(i -> {
//            try {
//                Thread.sleep(1);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            JvmCreateClassDTO jvmCreateClassDTO = new JvmCreateClassDTO();
//            jvmCreateClassDTO.setI(i);
//            jvmCreateClassDTO.setBytes(new byte[2 * _1MB]);
//            map.put(i, jvmCreateClassDTO);
        });
    }
}
